package com.zzw.controller;

import com.zzw.dto.AssignRoleDto;
import com.zzw.entity.Auth;
import com.zzw.entity.Result;
import com.zzw.entity.Role;
import com.zzw.entity.User;
import com.zzw.page.Page;
import com.zzw.service.AuthService;
import com.zzw.service.RoleService;
import com.zzw.service.UserService;
import com.zzw.entity.CurrentUser;
import com.zzw.utils.TokenUtils;
import com.zzw.utils.WarehouseConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Project: warehouse
 * @Description:
 * @Author: zzw
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private AuthService authService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    /**
     * 加载当前登录用户权限(菜单)树的url接口
     *
     * @param clientToken
     * @return
     */
    @GetMapping("/auth-list")
    @ApiOperation("加载当前登录用户权限(菜单)树")
    public Result authList(@RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String clientToken) {
        //从前端归还的token中解析出当前登录用户的信息
        CurrentUser currentUser = tokenUtils.getCurrentUser(clientToken);
        //根据用户id查询用户权限(菜单)树
        List<Auth> authTreeList = authService.findAuthTree(currentUser.getUserId());
        //响应
        return Result.ok(authTreeList);
    }

    /**
     * 分页查询用户的url接口
     *
     * @param page
     * @param user
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询用户")
    public Result userListPage(Page page, User user) {
        Page page1 = userService.queryUserPage(page, user);
        return Result.ok(page1);
    }

    /**
     * 添加用户的url接口
     *
     * @param user
     * @param token
     * @return
     */
    @PostMapping
    @ApiOperation("添加用户")
    public Result saveUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即创建新用户的用户id
        int createBy = currentUser.getUserId();
        user.setCreateBy(createBy);
        //执行业务
        return userService.saveUser(user);
    }

    /**
     * 修改用户状态的url接口
     *
     * @param user
     * @param token
     * @return
     */
    @PutMapping("/state")
    @ApiOperation("修改用户状态")
    public Result updateUserState(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即修改用户的用户id
        int updateBy = currentUser.getUserId();

        //设置修改用户的用户id和修改时间
        user.setUpdateBy(updateBy);
        user.setUpdateTime(new Date());

        //执行业务
        return userService.updateUserState(user);
    }

    /**
     * 修改用户的url接口
     *
     * @param user
     * @param token
     * @return
     */
    @PutMapping
    @ApiOperation("修改用户")
    public Result updateUser(@RequestBody User user, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        //获取当前登录的用户
        CurrentUser currentUser = tokenUtils.getCurrentUser(token);
        //获取当前登录的用户id,即修改用户的用户id
        int updateBy = currentUser.getUserId();

        //设置修改用户的用户id
        user.setUpdateBy(updateBy);

        //执行业务
        return userService.updateUserName(user);
    }

    /**
     * 删除用户的url接口
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    @ApiOperation("删除用户")
    public Result deleteUser(@PathVariable Integer userId) {
        return userService.deleteUserById(userId);
    }

    @DeleteMapping("/list")
    @ApiOperation("删除用户")
    public Result deleteUserBatch(@RequestBody List<Integer> userIds) {
        return userService.removeUserById(userIds);
    }

    /**
     * 查询用户已分配角色的url接口
     *
     * @param userId
     * @return
     */
    @GetMapping("/role/{userId}")
    @ApiOperation("查询用户已分配角色")
    public Result userRoleList(@PathVariable Integer userId) {
        List<Role> roleList = roleService.queryRolesByUserId(userId);
        return Result.ok(roleList);
    }

    //角色回显问题


    /**
     * 给用户分配角色的url接口
     *
     * @param assignRoleDto
     * @return
     */
    @PutMapping("/role")
    @ApiOperation("给用户分配角色")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto) {
        roleService.assignRole(assignRoleDto);
        return Result.ok("用户角色分配成功！");
    }

    /**
     * 重置密码的url接口
     *
     * @param userId
     * @return
     */
    @GetMapping("/pwd/{userId}")
    @ApiOperation("重置密码")
    public Result resetPassword(@PathVariable Integer userId) {
        return userService.resetPwd(userId);
    }

    //根据用户类型查询


    ///**
    // * 获取用户已有的权限
    // */
    //@GetMapping("/user-auth/{userId}")
    //public Result userAuth(@PathVariable Integer userId) {
    //    return Result.ok();
    //}


    // //批量删除用户
    //@DeleteMapping("/list")
    //public Result deleteUserList(){
    //    return Result.ok();
    //}

    //修改用户权限

    //导出数据
    @GetMapping("/exportTable")
    public Result exportTable(Page page, User user) {
        page = userService.queryUserPage(page, user);
        List<?> resultList = page.getResultList();
        //响应
        return Result.ok(resultList);
    }
}
